package template;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;

import net.java.ao.DatabaseProvider;
import net.java.ao.EntityManager;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

public class ShareEvent extends Restricted{
	//definition of local variables
	private String selectedEvent, shareUser;
	private HashMap<String, Integer> eventMap, userMap;
	
	//private class of form to override onSubmit
	private class ShareEventForm extends Form {
		private ShareEvent _shareEvent;
		public ShareEventForm(String id, ShareEvent shareEvent) {
			super(id);
			_shareEvent = shareEvent;
		}
		public void onSubmit() {
			try {
				_shareEvent.shareEvent();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public ShareEvent() {
		super();
		//instantiate hashmap
		eventMap = new HashMap<String, Integer>();
		
		//define user in current session
		Person user = (Person)_session.getUser();
		
		//create instance of form
		ShareEventForm form = new ShareEventForm("form", this);
		
		//create list of event names for dropdown
		LinkedList<String> eventNames = new LinkedList<String>();
		
		//propertmodels for the event selected in dropdown and the user-to-share
		PropertyModel eventNameModel = new PropertyModel(this, "selectedEvent");
		PropertyModel shareUserModel = new PropertyModel(this, "shareUser");
		
		//instance of entity manager
		EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
		
		//retrieve all calendars belonging to user and add the names of each event
		//on each calendar (excluding viewable/editable events) to the list for the dropdown
		//also add each event to the hashmap with name as key and ID as value
		try {
			Calendar[] usersCalendars = manager.find(Calendar.class, "creatorID = ?", user.getID());
			for(int i=2;i<usersCalendars.length;i++) {
				Event[] calendarEvents = usersCalendars[i].getEvents();
				for(int j=0;j<calendarEvents.length;j++) {
					eventNames.add(calendarEvents[j].getName());
					eventMap.put(calendarEvents[j].getName(), calendarEvents[j].getID());
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//add the dropdown and text field to the form
		form.add(new DropDownChoice("selectedEvent", eventNameModel, eventNames));
		form.add(new TextField("shareUser", shareUserModel));
		
		//add form to layout
		_layout.add(form);
		
	}
	//did not have time to implement
	public void shareEvent() throws SQLException {
		
		//instantiate hashmap for users with their ID in database
		userMap = new HashMap<String, Integer>();
		
		//instance of entity manager
    	EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
    	
    	//define user of current session
    	Person user = (Person)_session.getUser();
    	
    	//retrieval of all users from database
    	Person[] users = manager.find(Person.class);
    	
    	//load each user into hashmap with their username as key and ID as value
    	for(int i=0;i<users.length;i++)
    		userMap.put(users[i].getUsername(), users[i].getID());
    	
    	//finding of the user-to-share-with using the ID provided from hashmap
    	Person sharedUser = manager.get(Person.class, userMap.get(shareUser));
    	
    	//retrieval of the user-to-share-with's Viewable Events calendar
    	Calendar[] shareUserCalendar = manager.find(Calendar.class, "creatorID = ? and name = ?", sharedUser.getID(), "Viewable Events");
    	
    	//creating a relation to relate the event to the Editable Events calendar
    	EventsToCalendars relation = manager.create(EventsToCalendars.class);
    	
    	//retrieval of the event from database using eventID provided from hashmap
    	Event event = manager.get(Event.class, eventMap.get(selectedEvent));
    	
    	//setting the values of calendar and event in the EventsToCalendars relation and saving it
    	relation.setCalendar(shareUserCalendar[0]);
    	relation.setEvent(event);
    	relation.save();
    	
    	//redirecting user to the view calendars page
		this.setResponsePage(ViewCalendars.class);
	}
}
